// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var EventManager = require("event_manager");
cc.Class({
    extends: cc.Component,

    properties: {
        icon: cc.Sprite,
        itemType: 0,
    },

    onLoad: function() {
        this.lacked = false;

        EventManager.register(EventManager.START_EXPLORE, this, this.onExploreStart);
        EventManager.register(EventManager.END_EXPLORE, this, this.onExploreEnd);
    },

    // LIFE-CYCLE CALLBACKS:
    init: function(item, itemType) {
        var res = require("RES");
        this.itemType = itemType;
        this.item = item;

        if (this.item.iconPath) {
            console.log("this.item.iconPath = ", this.item.iconPath);
            res.changeSF(this.icon, this.item.iconPath, function() {
                cc.log("change res!");
            });
        }
    },

    getItem: function() {
        return this.item;
    },

    setItemType: function(type) {
        this.itemType = type;
    },

    onExploreStart: function() {
        this.lacked = true;
    },

    onExploreEnd:function() {
        this.lacked = false;
    },

    onClick: function() {
        // fire event;
        if (this.lacked) {
            alert("冒险中物品被锁定");
            return;
        }
        EventManager.fire(EventManager.ITEM_TYPE_CHANGE, this.itemType, this.item);
    }

    // update (dt) {},
});
